// pages/chongzhi/chongzhi.js

const getUserAccountUrl = require('../../utils/config').getUserAccountUrl;
const rechargeByWxUrl = require('../../utils/config').rechargeByWxUrl;
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {

    //账户余额
    account: '',

    items: [

      {
        name: 'weixin',
        value: '微信',
        checked: 'true'
      },

      // { name: 'zhifubao', value: '支付宝' },

    ],

    //充值金额
    chBalance: ''
  },

  radioChange: function (e) {

    console.log('radio发生change事件，携带value值为：', e.detail.value)

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.setNavigationBarTitle({
      title: "账户充值",
    });
  },

  getAccountBalanceByStuCode: function () {
    //账户余额
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: getUserAccountUrl,
      method: 'POST',
      header: {
        'Authorization':'Bearer '+ wx.getStorageSync('token')
      },
      success: function (res) {
        wx.hideLoading();
        console.log('??????' + res.data.data.balance)
        var list = res.data.data;
        console.log('+++++++' + list);
        if (list == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: toastText,
            image: '/images/erron.png',
            duration: 2000
          });
        } else {
          that.setData({
            account: list
          })

        }
      },
      fail:function(err){
          console.log("查询余额失败------"+err);
      }
    })
  },

  //获取充值金额
  chBalance: function (e) {
    this.setData({
      chBalance: e.detail.value
    })
  },
  //提交付款
  fukuan: function (e) {
    var chBalance = this.data.chBalance;
    if (chBalance != 0 && chBalance != ' ') {

      wx.request({
        url: rechargeByWxUrl,
        data: {
          amount: chBalance,
          source: "3",
          channelCode: "WX",
        },
        method: 'POST',
        header: {
          'Authorization':'Bearer '+ wx.getStorageSync('token'),
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var payargs = JSON.parse(res.data.data);
          wx.requestPayment({
            timeStamp: payargs.timeStamp,
            nonceStr: payargs.nonceStr,
            package: payargs.package,
            signType: payargs.signType,
            paySign: payargs.paySign,
            success: function (res) {
              wx.showToast({
                title:'支付成功！',
                icon: 'success',
                duration:1000
              })
              setTimeout(()=>{
                wx.reLaunch({url:'../../pages/index/index'})
              },1000)
              
            },
            fail: function (e) {},
            complete: function (e) {

            }
          })
        },
        fail:function(err){
            console.log("付款失败------"+err);
        }
      })

    } else {
      wx.showModal({
        title: '提示',
        content: '请输入充值金额',
        showCancel: false,
        success: function (res) {

        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    // 判断是否授权
    if (wx.getStorageSync('openId') != '') {
      if (wx.getStorageSync('token') == '' || wx.getStorageSync('token') == null) {
        wx.showToast({
          title: '你已授过权, 请登录',
        })
        wx.redirectTo({
          url: '../../pages/userlogin/login'
        })
      } else {

        that.getAccountBalanceByStuCode();
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '你还未授权登录，是否授权登录',
        cancelText: '取消',
        cancelColor: 'red',
        confirmText: '确认',
        confirmColor: '#30a1db',
        success: function (res) {
          //点击取消
          if (res.cancel) {

            wx.switchTab({
              url: '../../pages/index/index',
            })
          } else {
            //点击确定
            wx.redirectTo({
              url: '../../pages/userlogin/login'
            })
          }

        },
        fail: function () {}
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})